package com.GroupSix.controller;

import com.GroupSix.entity.PracticeEntity;
import com.GroupSix.service.PracticeService;
import com.GroupSix.vo.AutoAssessmentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("backend/practice")
public class PracticeController {
    //字段注入
    @Autowired
    private PracticeService practiceService;

    //增
    @PostMapping("add")
    public String add(@RequestBody PracticeEntity practiceEntity) {
        Integer i = practiceService.add(practiceEntity);
        if (i > 0) {
            return "已成功添加";
        } else {
            return "添加失败";
        }
    }

    //删
    @PostMapping("delete/{id}")
    public String delete(@PathVariable("id") int id) {
        Integer i = practiceService.delete(id);
        if (i > 0) {
            return "已成功删除";
        } else {
            return "删除失败";
        }
    }

    //查
    @PostMapping("find/{id}")
    public PracticeEntity find(@PathVariable("id") int id) {
        return practiceService.find(id);
    }

    //改
    @PostMapping("update")
    public String update(@RequestBody PracticeEntity practiceEntity) {
        Integer i = practiceService.update(practiceEntity);
        if (i > 0) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }

    //根据科目生成训练题
    //形参:科目,题目数量,学号
    @PostMapping("generatePractice")
    public PracticeEntity generatePractice(@RequestParam("course") String course, @RequestParam("number") Integer number,
                                           @RequestParam("studentId") Integer studentId) {
        return practiceService.generatePractice(course, number, studentId);
    }

    //根据正确率实时修改学生能力等级
    @PostMapping("autoAssessment")
    public String autoAssessment(@RequestBody AutoAssessmentVo vo) {
        Integer i = practiceService.autoAssessment(vo.getPracticeId(), vo.getStudentId(), vo.getAnswerCard());
        if (i > 0) {
            return "success";
        } else {
            return "fail";
        }
    }
}
